home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.awt.BorderLayout;
- import java.awt.Button;
- import java.awt.Checkbox;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Event;
- import java.awt.Panel;
- import java.util.StringTokenizer;
-
- public class Graph extends Applet {
- GraphPanel panel;
-
- public void init() {
- ((Container)this).setLayout(new BorderLayout());
- this.panel = new GraphPanel(this);
- ((Container)this).add("Center", this.panel);
- Panel p = new Panel();
- ((Container)this).add("South", p);
- ((Container)p).add(new Button("Scramble"));
- ((Container)p).add(new Button("Shake"));
- ((Container)p).add(new Checkbox("Stress"));
- ((Container)p).add(new Checkbox("Random"));
- String edges = ((Applet)this).getParameter("edges");
- StringTokenizer t = new StringTokenizer(edges, ",");
-
- while(t.hasMoreTokens()) {
- String str = t.nextToken();
- int i = str.indexOf(45);
- if (i > 0) {
- int len = 50;
- int j = str.indexOf(47);
- if (j > 0) {
- len = Integer.valueOf(str.substring(j + 1));
- str = str.substring(0, j);
- }
-
- this.panel.addEdge(str.substring(0, i), str.substring(i + 1), len);
- }
- }
-
- Dimension d = ((Component)this).size();
- String center = ((Applet)this).getParameter("center");
- if (center != null) {
- Node n = this.panel.nodes[this.panel.findNode(center)];
- n.x = (double)(d.width / 2);
- n.y = (double)(d.height / 2);
- n.fixed = true;
- }
-
- }
-
- public void start() {
- this.panel.start();
- }
-
- public void stop() {
- this.panel.stop();
- }
-
- public boolean action(Event evt, Object arg) {
- if (arg instanceof Boolean) {
- if (((Checkbox)evt.target).getLabel().equals("Stress")) {
- this.panel.stress = (Boolean)arg;
- } else {
- this.panel.random = (Boolean)arg;
- }
-
- return true;
- } else if ("Scramble".equals(arg)) {
- ((Applet)this).play(((Applet)this).getCodeBase(), "audio/computer.au");
- Dimension d = ((Component)this).size();
-
- for(int i = 0; i < this.panel.nnodes; ++i) {
- Node n = this.panel.nodes[i];
- if (!n.fixed) {
- n.x = (double)10.0F + (double)(d.width - 20) * Math.random();
- n.y = (double)10.0F + (double)(d.height - 20) * Math.random();
- }
- }
-
- return true;
- } else if ("Shake".equals(arg)) {
- ((Applet)this).play(((Applet)this).getCodeBase(), "audio/gong.au");
- ((Component)this).size();
-
- for(int i = 0; i < this.panel.nnodes; ++i) {
- Node n = this.panel.nodes[i];
- if (!n.fixed) {
- n.x += (double)80.0F * Math.random() - (double)40.0F;
- n.y += (double)80.0F * Math.random() - (double)40.0F;
- }
- }
-
- return true;
- } else {
- return false;
- }
- }
- }
-